﻿using CallApi.Extension;
using System;
using System.Collections.Generic;
using System.Text;

namespace CallApi.Model
{
    public class BaseApiModel
    {
        public string Sign { get; set; }

        public string RequestTime { get; set; }

        /// <summary>
        /// 设置签名
        /// </summary>
        /// <param name="appSecret">密钥</param>
        public virtual void SetSign(string privateKey = "")
        {
            string appSecret = privateKey;

            var signData = this.ToDictionary().RemoveKey("Sign");
            var signStr = signData.GetSortedContent(appSecret);
            Sign = signStr.ToMd5();
        }

        /// <summary>
        /// 检验签名
        /// </summary>
        /// <param name="appSecret">密钥</param>
        /// <returns>签名正确返回true</returns>
        public virtual bool CheckSign(string privateKey = "")
        {
            string appSecret = privateKey;

            var signData = this.ToDictionary().RemoveKey("Sign");
            var signStr = signData.GetSortedContent(appSecret);
            var sign = signStr.ToMd5();
            var isSuccess = string.Equals(sign, Sign);
            return isSuccess;
        }

    }
}
